package com.wpml.datasourcepool;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.util.List;

/**
 * @Description
 * @Date 2021-08-16
 * @Author wangpei
 **/
public class MyInvocationHandler implements InvocationHandler{
    Connection connection;

    volatile List<Connection> pool;

    public MyInvocationHandler(Connection connection, List<Connection> pool) {
        this.connection = connection;
        this.pool = pool;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (method.getName().equals("close")) {
            pool.add(connection);
            System.out.println("close after size: " + pool.size());
        }
        return method.invoke(connection, args);
    }
}
